﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using CommonLib;

namespace CommonLib
{
    public class EntitySessionFactory
    {
        private static ISessionFactory sessionFactory;

        public EntitySessionFactory(System.Reflection.Assembly assembly)
        {
            Configuration cfg = new Configuration();
            cfg.Configure(@"D:\Organic\Pruebas\HormigaNHVfp\OrganicNetNH.config");
            cfg.AddAssembly(assembly);
            try
            {
                sessionFactory = cfg.BuildSessionFactory();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debugger.Launch();
                throw ex;
            }
        }

        public EntitySession Create()
        {
            ISession session = sessionFactory.OpenSession();
            session.FlushMode = FlushMode.Commit;
            
            return new EntitySession(session);
        }
    }
}
